import type { ResponsePaginationData} from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import type { QueryListParams, TableListItem, UserItem } from './data';

/**
 * 获取会员用户列表
 * @param params
 */
export async function queryMemberUserList(
  params: Pagination & QueryListParams,
): Promise<RequestData<TableListItem>> {
  const { page = 1, pageSize = 20, nickname, gender } = params;

  const res: ResponsePaginationData<UserItem> = await request('/member/user/', {
    params: { page, pageSize, nickname, gender },
  });

  if (res?.code !== SUCCESS_CODE) {
    return { success: false, total: 0, data: [] };
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

export async function queryUserDetail(params: {
  id: string;
}): Promise<{ success: boolean; data?: UserItem }> {
  const res = await request(`/sys/admin/${params.id}`);

  if (res?.code !== SUCCESS_CODE) {
    return { success: false };
  }

  return {
    success: true,
    data: res.data,
  };
}
